package com.qf.service.impl;

import cn.hutool.core.lang.tree.Node;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.qf.entity.Product;
import com.qf.entity.ProductCategory;
import com.qf.entity.ProductCategoryVO;
import com.qf.mapper.ProductCategoryMapper;
import com.qf.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * --- 万物归集  不随你我 ---
 * <p>
 * 快捷短语
 * main ==  public static void main(String[] args);
 * sout ==  System.out.println();
 *
 * @author shuhan
 * @date 2025/4/29
 * @desc
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<Tree<String>> list() {

        List<ProductCategoryVO> productCategoryVOList = productCategoryMapper.selectAllItem();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> treeNodes = TreeUtil.build(productCategoryVOList, "0", treeNodeConfig,
                (treeNode, tree) -> {
            tree.setId(treeNode.getId().toString());
            tree.setParentId(treeNode.getParentId().toString());
            tree.putExtra("name",treeNode.getName());
            tree.putExtra("sortOrder",treeNode.getSortOrder());
            tree.putExtra("createdAt",treeNode.getCreatedAt());
            tree.putExtra("parentId",treeNode.getParentId().toString());
            tree.putExtra("children",treeNode.getChildren());

        });

        return treeNodes;
    }

    @Override
    public void delete(Integer id) {
        productCategoryMapper.delete(id);
    }

    @Override
    public void add(ProductCategory productCategory) {
        productCategory.setCreatedAt(new Date());
        productCategory.setParentId(0L);
        ProductCategory lastProductCategory = productCategoryMapper.getlastId();
        Long id = lastProductCategory.getId();
        id++;
        productCategory.setId(id);
        productCategoryMapper.insert(productCategory);
    }

    @Override
    public String findNameById(Integer id) {
        ProductCategory productCategory = productCategoryMapper.selectById(id);
        String name = productCategory.getName();
        return name;
    }

    @Override
    public void update(ProductCategory productCategory) {
        productCategoryMapper.update(productCategory);
    }


}
